package com.xilihui.xlh.business.adapters;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.mylhyl.zxing.scanner.QRCode;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.activitys.MergePaymentsActivity;
import com.xilihui.xlh.business.activitys.StartAppointmentActivity;
import com.xilihui.xlh.business.activitys.TakeGoodsActivity;
import com.xilihui.xlh.business.activitys.store.ConfirmOrderActivity;
import com.xilihui.xlh.business.activitys.store.OrderDetailsActivity;
import com.xilihui.xlh.business.activitys.store.PostEvaluationActivity;
import com.xilihui.xlh.business.activitys.store.QRcodeActivity;
import com.xilihui.xlh.business.activitys.store.TradeSuccessActivity;
import com.xilihui.xlh.business.dialogs.PayDialog;
import com.xilihui.xlh.business.entities.AppointmentEntity;
import com.xilihui.xlh.business.entities.PayEntity;
import com.xilihui.xlh.business.entities.PublicOrderEntity;
import com.xilihui.xlh.business.requests.AppointmentRequest;
import com.xilihui.xlh.business.requests.MyAppointmentRequest;
import com.xilihui.xlh.business.requests.StoreRequest;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.app.BaseEntity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import org.feezu.liuli.timeselector.Utils.TextUtil;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/6/26.
 */

public class StoreOrderAdapter extends RecyclerView.Adapter<BaseViewHolder>{
    
    private static final int merge = 1;
    private static final int one = 2;
  
    
    int status = 0;
    FragmentActivity activity;
    ArrayList<PublicOrderEntity> datas = new ArrayList<>();
    
    
    public StoreOrderAdapter(FragmentActivity fragmentActivity, int status){
        this.activity = fragmentActivity;
        this.status = status;
    }
    
    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType){
            case merge:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_store_merge_order,
                        parent,false);
                break;
            case one:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_store_order,
                        parent,false);
                break;
        }
        return new BaseViewHolder(view);
    }
    
    
    
    @Override
    public void onBindViewHolder(BaseViewHolder holder, final int position) {

        switch (getItemViewType(position)){
            case merge:
                RecyclerView recyclerView = (RecyclerView)holder.getView(R.id.recyclerview);
                recyclerView.setLayoutManager(new LinearLayoutManager(activity));
                
                ArrayList<PublicOrderEntity.OrderBean> items = new ArrayList<>();
                items = (ArrayList<PublicOrderEntity.OrderBean>) datas.get(position).getOrder();
                BaseAdapter<PublicOrderEntity.OrderBean> baseAdapter = new BaseAdapter<PublicOrderEntity.OrderBean>(activity,
                        items) {
                    @Override
                    protected int setLayoutId() {
                        return R.layout.recycler_item_shop_bill;
                    }

                    @Override
                    protected void getView(BaseViewHolder holder, PublicOrderEntity.OrderBean entity, int pos) {
                        SimpleDraweeView simpleDraweeView = (SimpleDraweeView)holder.getView(R.id.iv_img);
                        simpleDraweeView.setImageURI(UrlConst.baseUrl()+entity.getOrder_goods().getOriginal_img());
                        
                        holder.setText(R.id.tv_name,entity.getOrder_goods().getGoods_name());
                        holder.setText(R.id.tv_model,entity.getOrder_goods().getSpec_key_name());
                        holder.setText(R.id.tv_price,"￥"+entity.getOrder_goods().getGoods_price());
                        holder.setText(R.id.tv_num,"x"+entity.getOrder_goods().getGoods_num());
                        
                        holder.itemView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent intent = new Intent(activity, OrderDetailsActivity.class);
                                intent.putExtra("type",1);
                                intent.putExtra("order_all_id",datas.get(position).getId());
                                intent.putExtra("status",status);
                                activity.startActivity(intent);
                            }
                        });
                        
                    }
                };
                
                
                recyclerView.setAdapter(baseAdapter);
                baseAdapter.setAnimationsLocked(true);
                holder.setText(R.id.tv_ziti,"");
                holder.setText(R.id.tv_num,"共"+datas.get(position).getCount()+"件商品 合计：");
                holder.setText(R.id.tv_total_price,"￥"+datas.get(position).getMoney());
                
                holder.setOnClickListener(R.id.tv_cancle, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        StoreRequest.cancelOrder(activity,datas.get(position).getOrder().get(0).getOrder_all_id())
                                .compose(DoTransform.<BaseEntity>applyScheduler(activity,true))
                                .subscribe(new HttpSubscriber<BaseEntity>(activity) {
                                    @Override
                                    protected void onSuccess(BaseEntity entity) {
                                        ToastUtil.toastShortPositive(entity.getMsg());
                                        YEventBuses.post(new YEventBuses.Event("store_order_cancel"));
                                    }

                                    @Override
                                    protected void onFail(String msg) {
                                        ToastUtil.toastShortNegative(msg);
                                    }
                                });
                    }
                });
                
                holder.setOnClickListener(R.id.tv_pay, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        PayDialog payDialog = new PayDialog(activity);
                        payDialog.setOnItemSelectListener(new PayDialog.OnItemSelectListener() {
                            @Override
                            public void close() {

                            }

                            @Override
                            public void alipay() {
                                pay(datas.get(position).getOrder_all_sn(),"alipay");
                            }

                            @Override
                            public void weixin() {
                                pay(datas.get(position).getOrder_all_sn(),"weixin");
                            }

                        });

                        payDialog.show();
                    }
                });
                

                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(activity, OrderDetailsActivity.class);
                        intent.putExtra("type",1);
                        intent.putExtra("order_all_id",datas.get(position).getId());
                        activity.startActivity(intent);
                    }
                });
                
                
                break;
            case one:

                TextView textView = (TextView)holder.getView(R.id.tv_ziti);
                textView.setCompoundDrawables(null,null,null,null);
                textView.setText("快递包邮");
                
                holder.setText(R.id.tv_status,datas.get(position).getTitle());
                
                String order_status = datas.get(position).getType_status();
                if(order_status.equals("dfh")){
//                    holder.setText(R.id.tv_status,"待发货");
                    holder.setVisibility(R.id.tv_cancle,View.GONE);
                    holder.setVisibility(R.id.tv_pay,View.GONE);
                    
                }else if(order_status.equals("dsh")){
//                    holder.setText(R.id.tv_status,"待收货");
                    holder.setVisibility(R.id.tv_cancle,View.INVISIBLE);

                    final String ziti = datas.get(position).getShipping_code();
                    
                    holder.setOnClickListener(R.id.tv_pay, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if(ziti.equals("ziti")){
                                Intent intent = new Intent(activity, QRcodeActivity.class);
                                intent.putExtra("order_id",datas.get(position).getOrder_id());
                                activity.startActivity(intent);
                            }else {
                                StoreRequest.confirmReceipt(activity,datas.get(position).getOrder_id())
                                        .compose(DoTransform.<BaseEntity>applyScheduler(activity,true))
                                        .subscribe(new HttpSubscriber<BaseEntity>(activity) {
                                            @Override
                                            protected void onSuccess(BaseEntity entity) {
//                                            ToastUtil.toastShortPositive(entity.getMsg());
                                                YEventBuses.post(new YEventBuses.Event("querenshouhuo"));
                                                Intent intent = new Intent(activity, TradeSuccessActivity.class);
                                                intent.putExtra("type",1);
                                                activity.startActivity(intent);
                                            }

                                            @Override
                                            protected void onFail(String msg) {
                                                ToastUtil.toastShortNegative(msg);
                                            }
                                        });
                                
                            }
                        }
                    });
                    
                }else if(order_status.equals("dpj")||order_status.equals("jycg")||order_status.equals("ypj")){
                    
                    holder.setText(R.id.tv_cancle,"删掉订单");
                    holder.setText(R.id.tv_pay,"评价");
//                    holder.setText(R.id.tv_status,"交易成功");
                    if(order_status.equals("ypj")){
                        holder.setVisibility(R.id.tv_pay,View.GONE);
                        holder.setVisibility(R.id.tv_cancle,View.VISIBLE);
                    }else {
                        holder.setVisibility(R.id.tv_cancle,View.VISIBLE);
                        holder.setVisibility(R.id.tv_pay,View.VISIBLE);
                    }
                    
                    holder.setOnClickListener(R.id.tv_cancle, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            StoreRequest.deleteOrder(activity,datas.get(position).getOrder_id())
                                    .compose(DoTransform.<BaseEntity>applyScheduler(activity,true))
                                    .subscribe(new HttpSubscriber<BaseEntity>(activity) {
                                        @Override
                                        protected void onSuccess(BaseEntity entity) {
                                            ToastUtil.toastLongPositive(entity.getMsg());
                                            YEventBuses.post(new YEventBuses.Event("delete_cancel"));
                                        }

                                        @Override
                                        protected void onFail(String msg) {
                                            ToastUtil.toastShortNegative(msg);
                                        }
                                    });
                        }
                    });
                    
                    holder.setOnClickListener(R.id.tv_pay, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(activity, PostEvaluationActivity.class);
                            intent.putExtra("order_id",datas.get(position).getOrder_id());
                            intent.putExtra("image",datas.get(position).getOrder_goods().getOriginal_img());
                            activity.startActivity(intent);
                        }
                    });
                    
                }
                
                final String ziti = datas.get(position).getShipping_code();
                if(ziti.equals("ziti")){
                    holder.setText(R.id.tv_pay,"领货码");
                    holder.setText(R.id.tv_ziti,"门店自提");
                }else {
                    holder.setText(R.id.tv_pay,"确认收货");
                    holder.setText(R.id.tv_ziti,"快递包邮");
                }
                
                SimpleDraweeView simpleDraweeView = (SimpleDraweeView)holder.getView(R.id.sdv);
                simpleDraweeView.setImageURI(UrlConst.baseUrl()+datas.get(position).getOrder_goods().getOriginal_img());
                holder.setText(R.id.tv_name,datas.get(position).getOrder_goods().getGoods_name());
                holder.setText(R.id.tv_price,"￥"+datas.get(position).getOrder_goods().getGoods_price());
                holder.setText(R.id.tv_model,datas.get(position).getOrder_goods().getSpec_key_name());
                holder.setText(R.id.tv_goodNum,"x"+datas.get(position).getOrder_goods().getGoods_num());
                
                holder.setText(R.id.tv_num,"共"+datas.get(position).getOrder_goods().getGoods_num()+"件商品 合计：");
                holder.setText(R.id.tv_total_price,"￥"+datas.get(position).getOrder_goods().getGoods_price());
                
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(activity, OrderDetailsActivity.class);
                        intent.putExtra("type",2);
                        intent.putExtra("order_id",datas.get(position).getOrder_id());
                        intent.putExtra("status",status);
                        activity.startActivity(intent);
                    }
                });
                
                break;
                
        }
        
    }

    @Override
    public int getItemCount() {
        if(datas!=null&&datas.size()>0){
            return datas.size();
        }else {
            return 0;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(datas.get(position).getType()==1){
            return merge;
        }else {
            return one;
        }
    }
    
    public void setData(ArrayList<PublicOrderEntity> data){
        if(datas.size()>0){
            datas.clear();
        }
        
        datas.addAll(data);
        notifyDataSetChanged();
    }
    
    public void pay(final String sign_sn, final String code){
        StoreRequest.pay(activity,sign_sn,code)
                .compose(DoTransform.<PayEntity>applyScheduler(activity,true))
                .subscribe(new HttpSubscriber<PayEntity>(activity) {
                    @Override
                    protected void onSuccess(PayEntity ent) {
                        if(code.equals("alipay")){
                            YEventBuses.post(new YEventBuses.Event("store_pay").setParams(code,ent.getAli_payment_params()));
                        }else {
                            YEventBuses.post(new YEventBuses.Event("store_pay").setParams(code,ent));
                        }
                    }
                    
                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });
    }
    
    //取消预约
    public void cancle(final View view, final String id){
            
        AppointmentRequest.hint(activity,id)
                .compose(DoTransform.<BaseEntity>applyScheduler(activity,true))
                .subscribe(new HttpSubscriber<BaseEntity>(activity) {
                    @Override
                    protected void onSuccess(BaseEntity entity) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                        builder.setMessage(entity.getMsg());
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                MyAppointmentRequest.cancelAppoint(view.getContext(),id)
                                        .compose(DoTransform.<BaseEntity>applyScheduler(activity,true))
                                        .subscribe(new HttpSubscriber<BaseEntity>(activity) {
                                            @Override
                                            protected void onSuccess(final BaseEntity entity) {
                                                ToastUtil.toastShortPositive(entity.getMsg());
                                                LogUtil.i("mylog","quxiao");
                                                
                                            }

                                            @Override
                                            protected void onFail(String msg) {
                                                ToastUtil.toastShortNegative(msg);
                                            }
                                        });
                            }
                        });

                        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        });

                        builder.show();
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });
        
        
        
    }
    
    //到店取消
    public void daoDianCancle(final  View view, final  String id, final int position){
        
        MyAppointmentRequest.paying(activity,id)
                .compose(DoTransform.<BaseEntity>applyScheduler(activity,true))
                .subscribe(new HttpSubscriber<BaseEntity>(activity) {

                    @Override
                    protected void onSuccess(BaseEntity entity) {
                        if(TextUtil.isEmpty(entity.getMsg())){
                            Intent intent = new Intent(view.getContext(),StartAppointmentActivity.class);
                            intent.putExtra("data",datas.get(position));
                            intent.putExtra("status",2);
                            view.getContext().startActivity(intent);
                        }else {
                            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                            builder.setMessage(entity.getMsg());
                            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Intent intent = new Intent(view.getContext(),StartAppointmentActivity.class);
                                    intent.putExtra("data",datas.get(position));
                                    intent.putExtra("status",2);
                                    view.getContext().startActivity(intent);
                                }
                            });
                            
                            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    
                                }
                            });

                            builder.show();
                        }
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });
        
    }
    
    
}
